package com.lrr.community.controller;

import com.lrr.community.dto.PaginationDTO;
import com.lrr.community.mapper.UserMapper;
import com.lrr.community.model.User;
import com.lrr.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ProfileController {



    @Autowired
    private QuestionService questionService;

    // 通过url获取参数
    @GetMapping("/profile/{action}")
    public String profile(@PathVariable(name = "action")String action,
                          Model model,
                          HttpServletRequest request,
                          @RequestParam(name = "page",defaultValue = "1") Integer page,
                          @RequestParam(name = "size",defaultValue = "2") Integer size
    ){

        User user =(User) request.getSession().getAttribute("user");
        if(user==null)
            return "redirect:/";
        if(action.equals("questions")) {
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName","我的提问");
        }
        else if(action.equals("repies")){
            model.addAttribute("section", "repies");
            model.addAttribute("sectionName","我的回复");
        }
        PaginationDTO paginationDTO = questionService.list(user.getId(), page, size);
        model.addAttribute("pagination",paginationDTO);
        return "profile";
    }
}
